<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Spike extends ShopFrontEndController{

	public function __construct($params) {
		parent::__construct($params);
		$this->load->library('mypaging');
	}

	public function renderView($viewName, $data = null, $custom_layout=null){
		$theme_path = 'shop-themes/'. $this->shop->template;
		$this->load->library('frontend_lib', array('_ci'=>$this));
		$data['shop']        = $this->shop;
		$data['shop_page']   = $this->shop_model->getShopPages($this->shop->show_page);
		$data['shop_cate']   = $this->shop_model->getAllShopCategory($this->shop->getSlug());
		$data['shop_slides'] = $this->shop_model->getBannerSlide($this->shop->getSlug());
		$data['html']        = $this->load->view($theme_path . '/pages/' . $viewName . '.html.php', $data, true);
		if($custom_layout!=NULL){
			$this->load->view($theme_path . '/layout/'.$custom_layout.'.html.php', $data);
		} else {
			$this->load->view($theme_path . '/layout/layout.html.php', $data);
		}
	}

	public function index() {
		$aReturn = array('pageTitle' => $this->shop->getTitle());
		$this->renderView('index', $aReturn);
	}

	private function intialPagingSearch($searchkey) {
		$this->mypaging->setLimit($this->shop_product_num);
		$this->mypaging->setSuffix($this->config->item('url_suffix'));
		$this->mypaging->setCount($this->shop_product_model->countFrontSearchProduct($this->shop->getId(), $searchkey));
		$this->mypaging->setMainURL(shop_product_search_paging_url($this->shop->getSlug()));
	}

	public function search($shop_slug, $pageIndex=1) {
		$searchkey    = $this->input->get('s', true);
		$this->intialPagingSearch($searchkey);
		$pagingBar    = $this->mypaging->getBar($pageIndex, array('s' => $searchkey));
		$offset       = ($pageIndex - 1) * $this->shop_product_num;
		$shopProducts = $this->shop_product_model->searchFrontProduct($this->shop->getId(), $searchkey, $this->shop_product_num, $offset);
		$aReturn      = array(
			'pageTitle'      => 'Sản phẩm',
			'shopProducts'   => $shopProducts,
			'pagingBar'      => $pagingBar,
			'searchkey'      => $searchkey,
			'class_instance' => $this
			);
		$this->renderView('index', $aReturn);
	}

	public function getTopBuyProduct($limit=PRODUCT_LIMIT, $isModule=false) {
		$aProduct = $this->shop_product_model->getTopBuyProducts($this->shop->getId(), $limit);
		$aReturn  = array('aProduct' => $aProduct);
		if($isModule){
			$this->getModule('top-buy', $aReturn);
		} else {
			$this->renderView('modules/top-buy', $aReturn);
		}
	}

	public function contact($shop_slug) {
		$aReturn = array('pageTitle'=>'Liên hệ');
		$this->renderView('contact', $aReturn, 'layout-no-sidebar');
	}

	public function login(){
		$aReturn = array('pageTitle'=>'Đăng nhập | '.$this->shop->getTitle());
		$this->renderView('login', $aReturn, 'layout-no-sidebar');
	}

	public function signup(){
		$aReturn = array('pageTitle'=>'Đăng kí | '.$this->shop->getTitle());
		$this->renderView('signup', $aReturn, 'layout-no-sidebar');
	}
}